package JUC_stady;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.*;
import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.Collectors;

/**
 * Project: tobe-better-coder
 * Package: JUC_stady
 *
 * @author : Mr.xu
 * @date : 2023/3/4 21:07
 */
public class BuyBook {

    private static List<Mall> malls;

    private static ExecutorService executors = Executors.newFixedThreadPool(35);

    static {
        malls = Arrays.asList(
                new Mall("京东"),
                new Mall("拼多多"),
                new Mall("当当"),
                new Mall("京东"),
                new Mall("拼多多"),
                new Mall("当当"),
                new Mall("京东"),
                new Mall("拼多多"),
                new Mall("当当"),
                new Mall("京东"),
                new Mall("拼多多"),
                new Mall("当当"),
                new Mall("京东"),
                new Mall("拼多多"),
                new Mall("当当"),
                new Mall("京东"),
                new Mall("拼多多"),
                new Mall("当当"),
                new Mall("京东"),
                new Mall("拼多多"),
                new Mall("当当"),
                new Mall("京东"),
                new Mall("拼多多"),
                new Mall("当当"),
                new Mall("京东"),
                new Mall("拼多多"),
                new Mall("当当"),
                new Mall("京东"),
                new Mall("拼多多"),
                new Mall("当当"),
                new Mall("京东"),
                new Mall("拼多多"),
                new Mall("当当"),
                new Mall("京东"),
                new Mall("拼多多"),
                new Mall("当当")
        );
    }

    public static List<String> getAllPrice(List<Mall> malls, String bookName) {
        return malls.stream().map(Mall -> String.format(bookName + " in %s price is %.2f ", Mall.getName(), Mall.getPrice())).collect(Collectors.toList());
    }

    public static List<String> getAllPriceFast(List<Mall> malls, String bookName) {
        try {
            return malls.stream()
                    .map(Mall -> CompletableFuture.supplyAsync(() -> String.format(bookName + " in %s price is %.2f ", Mall.getName(), Mall.getPrice()), executors))
                    .collect(Collectors.toList()).stream().map(CompletableFuture::join)
                    .collect(Collectors.toList());
        } finally {
            executors.shutdown();
        }
    }

    public static void main(String[] args) {
        long startTime = System.currentTimeMillis();
//        System.out.println(getAllPrice(malls,"mysql"));
        System.out.println(getAllPriceFast(malls, "mysql"));
        System.out.println("耗时" + (System.currentTimeMillis() - startTime) + "毫秒");
    }

}

class Mall {
    public Mall(String name) {
        this.name = name;
    }

    private String name;

    public String getName() {
        return name;
    }

    ReentrantLock l = new ReentrantLock(true);

    public synchronized void setName(String name) {
        this.name = name;
    }

    private double price;

    public double getPrice() {
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ThreadLocalRandom.current().nextDouble(90, 100);
    }

}